#!/bin/bash

set -o pipefail

#
# This script will install grctl tools
#

REPO_VER=${1}

[ -d /etc/goodrain/envs/ ] || mkdir -p /etc/goodrain/envs/

# check os-release ,now support CentOS 7.x, Maybe support Ubuntu 16.04 & Debian 9
RELEASE_INFO=$(cat /etc/os-release | grep "^VERSION=" | awk -F '="' '{print $2}' | awk '{print $1}' | cut -b 1-5)
if [[ $RELEASE_INFO == "7" ]];then
    OS_VER='centos/7'
elif [[ $RELEASE_INFO =~ "14" ]];then
    OS_VER='ubuntu/trusty'
elif [[ $RELEASE_INFO =~ "16" ]];then
    OS_VER='ubuntu/xenial'
elif [[ $RELEASE_INFO =~ "9" ]];then
    OS_VER="debian/stretch"
else
    echo "Release $(cat /etc/os-release | grep "PRETTY" | awk -F '"' '{print $2}') Not supported"
    exit 1
fi

function check_rbd_json() {
    if [[ $OS_VER =~ '7' ]];then
        yum install -y wget curl epel-release
        sed -e 's!^mirrorlist=!#mirrorlist=!g' \
         -e 's!^#baseurl=!baseurl=!g' \
         -e 's!//download\.fedoraproject\.org/pub!//mirrors.ustc.edu.cn!g' \
         -e 's!http://mirrors\.ustc!https://mirrors.ustc!g' \
         -i /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel-testing.repo
        
        yum install -y jq

        wget http://repo.goodrain.com/release/3.4.1/gaops/jobs/install/prepare/rbd.json -qO /etc/goodrain/envs/rbd.json
        REPO_VER=$(jq --raw-output '.mirrors.version' /etc/goodrain/envs/rbd.json)
    else
        apt install -y wget curl
        echo "not support now"
        exit 1
    fi    
}

function config_mirrors(){

    if [[ $OS_VER =~ '7' ]];then
            echo "Configure yum repo..."
            cat >/etc/yum.repos.d/acp.repo <<EOF
[goodrain]
name=goodrain CentOS-\$releasever - for x86_64
baseurl=http://repo.goodrain.com/centos/\$releasever/${REPO_VER}/\$basearch
enabled=1
gpgcheck=1
gpgkey=http://repo.goodrain.com/gpg/RPM-GPG-KEY-CentOS-goodrain
EOF
            yum makecache
            yum install -y git perl telnet bind-utils htop dstat net-tools lsof iproute rsync lvm2
        else
            log.info "Configure apt sources.list..."
            if [[ $OS_VER =~ '16' ]];then
                echo deb http://repo.goodrain.com/ubuntu/16.04 ${REPO_VER} main | tee /etc/apt/sources.list.d/acp.list 
            elif [[ $OS_VER =~ '16' ]];then
                echo "Not currently supported"
                exit 1
            else
                echo deb http://repo.goodrain.com/ubuntu/14.04 ${REPO_VER} main | tee /etc/apt/sources.list.d/acp.list                 
            fi
            curl http://repo.goodrain.com/gpg/goodrain-C4CDA0B7 2>/dev/null | apt-key add - \
            && apt update \
            && apt install  -y lsof htop rsync net-tools telnet iproute
    fi
}

function connfig_ssh_key() {
    
    [ -f "/root/.ssh/id_rsa.pub" ] && (
        grep $(cat /root/.ssh/id_rsa.pub | awk '{print $3}') /root/.ssh/authorized_keys >/dev/null
        if [ $? -ne 0 ];then
            cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
        fi
    ) || (
        ssh-keygen -t rsa -f /root/.ssh/id_rsa -q -N "" > /dev/null
        if [ $? -eq 0 ];then
             cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
        else
            echo ""
        fi
    )
    echo "config ssh"
}

function test_public() {
    # 获取公网ip
    pubic_ip=$(ip ad | grep 'inet ' | grep -vE '( 10.|172.|192.168|127.)' | awk '{print $2}' | cut -d '/' -f 1)
    if [ ! -z $pubic_ip ];then
        echo $pubic_ip > /etc/goodrain/envs/.exip
    fi
}

function install_grctl() {

    check_rbd_json

    config_mirrors

    connfig_ssh_key 

    test_public

    if [[ $OS_VER =~ '7' ]];then
        yum install gr-rainbond-grctl -y
    else
        apt install gr-rainbond-grctl -y
    fi
}

case $1 in
    * )
        install_grctl
        ;;
esac
